package com.ljh.Controller;

import com.ljh.mapper.MenuMapper;
import com.ljh.mapper.TypeMapper;
import com.ljh.pojo.Menu;
import com.ljh.pojo.MenuVo;
import com.ljh.pojo.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private TypeMapper typeMapper;



    @GetMapping("/findAll/{page}/{limit}")
    public MenuVo findAll(@PathVariable("page") int page, @PathVariable("limit") int limit){
        MenuVo menuVO = new MenuVo();
        menuVO.setCode(0);
        menuVO.setMsg("");
        menuVO.setCount(menuMapper.count());
        menuVO.setData(menuMapper.findAll((page-1)*limit,limit));
        return menuVO;
    }

    @GetMapping("/delete/{id}")
    public void delete(@PathVariable("id") long id){
        menuMapper.deleteById(id);
    }
    @PostMapping("/add")
    public void add(@RequestBody Menu menu){
        menuMapper.save(menu);
    }
    @GetMapping("/findById/{id}")
    public Menu findById(@PathVariable("id") long id){
        return menuMapper.findById(id);
    }
    @PutMapping("/update")
    public void update(@RequestBody Menu menu){
        menuMapper.update(menu);
    }
    @RequestMapping("/findTypes")
    public List<Type> findTypes(){
        return typeMapper.findAll();
    }

}
